<?php

namespace app\admin\controller\kuerp;

use app\common\controller\Backend;
use app\admin\model\department\Admin;
use app\admin\model\kuerp\StaffMember;
use app\admin\model\kuerp\GoodsType;
use \app\admin\model\department\Department as DepartmentModel;
use think\Db;
use fast\Tree;
/**
 * 销售汇总管理
 *
 * @icon fa fa-circle-o
 */
class GoodsSalesSummary extends Backend
{

    /**
     * GoodsSalesSummary模型对象
     * @var \app\admin\model\kuerp\GoodsSalesSummary
     */
    protected $model = null;
    protected $childrenSales = [];

    public function _initialize()
    {
        parent::_initialize();
        $setData = (new Goods())->getSetData();
        $this->assign('set', $setData['setList']);
        $this->assignconfig('set',$setData['setList']);
        $this->assign('nowmenu', $setData['nowmenu']);

        $this->model = new \app\admin\model\kuerp\GoodsSalesSummary;
        $this->view->assign("purchaseTypeList", $this->model->getPurchaseTypeList());
        $this->view->assign("payStatusList", $this->model->getPayStatusList());
        $this->view->assign("checkStatusList", $this->model->getCheckStatusList());
        $this->view->assign("examineStatusList", $this->model->getExamineStatusList());
        //部门经理查看部门成员 上级部门能查看下级部门
        $adminids = (new Admin())->getChildrenAdminIds($this->auth->id, true);
        //转为职员id
        $this->childrenSales = $adminids ? (new StaffMember())->getStaffMemberIds($adminids) : [];

        $this->view->assign("customers", $this->model->getCustomersList());
        $salelist = (new StaffMember())->getSaleList($this->childrenSales);
        $this->view->assign("saleList", $salelist);
        $saleLists = [];
        foreach ($salelist as $salelist_k=>$salelist_v){
            $saleLists[] = ['name'=>$salelist_v['name'],'value'=>$salelist_v['id']];
        }
        $this->assignconfig("saleLists", $saleLists);

        $tree = Tree::instance();
        $list = collection((new GoodsType())->order('weigh desc,id desc')->where(['status'=>'1'])->field('id,pid,supplier_type_name as title')->select())->toArray();
        $tree->init($list, 'pid');
        $catelist = $tree->getTreeList($tree->getTreeArray(0), 'title');
        // $catedata = [0 => ['type' => 'all', 'title' => __('请选择分类')]];
        foreach ($catelist as $k => $v) {
            $catedata[$v['id']] = $v;
        }
        $this->view->assign("goodsTypeList", $catedata);

        // $tree->init(Admin::getAllDepartmentsArray($this->auth->id,($this->auth->isSuperAdmin()||$this->auth->data_scope)?true:false), 'parent_id');
        // $departmentList = $tree->getTreeList($tree->getTreeArray(0), 'name');
        $orgindepartmentList = Admin::getAllDepartmentsTreeArray($this->auth->id, $this->auth->isSuperAdmin()||$this->auth->data_scope);
        $departmentList = [];
        foreach ($orgindepartmentList as $key => $value) {
            $departmentList[] = [
                'id' => $key,
                'name' => $value
            ];
        }
        $this->view->assign("departmentList", $departmentList);
    }

    public function import()
    {
        parent::import();
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            $where = [];
            $searchdata = json_decode(input('filter'),true);
            if($searchdata){
                foreach ($searchdata as $k => &$v) {
                    if(!empty($v)){
                        if(in_array($k,['goods_type','department_id'])) {
                            if ($k == 'goods_type') {
                                $where['gt.id'] = ['exp', Db::raw(' ='.$v.' OR FIND_IN_SET('. $v .',gt.path)')];
                            } else if ($k == 'department_id') {
                                $departmentids = DepartmentModel::getChildrenIds($v, true);
                                $adminids = Admin::getDepartmentAdminIds($departmentids);
                                $sales = (new StaffMember())->getStaffMemberIds($adminids);
                                $where['i.sale_user'] = ['in', $sales];
                            }
                        } else {
                            if(strpos($v,' - ') !== false && strpos($k,'time') !== false){
                                $str = explode(' - ',$v);
                                $where['i.'.$k] = ["BETWEEN", [strtotime($str[0]),strtotime($str[1])]];
                            }else if ($k=='goods_type'){
                                $where['gt.id'] = $v;
                            }else if ($k=='sale_user'){
                                $where['i.sale_user'] = ['in', $v];
                            }else if ($k=='goods_code'){
                                $where['i.goods_code|g.goods_name'] = ["LIKE", "%{$v}%"];
                            }else{
                                $where['i.'.$k] = ["LIKE", "%{$v}%"];
                            }
                        }
                        
                    }
                }
            }
            if (!isset($where['i.sale_user']) && !$this->auth->isSuperAdmin()) {
                //部门经理查看部门成员 上级部门能查看下级部门
                $adminids = (new Admin())->getChildrenAdminIds($this->auth->id, true);
                //转为职员id
                if ($adminids) {
                    $sales = (new StaffMember())->getStaffMemberIds($adminids);
                    $where['i.sale_user'] = ['in', $sales];
                }
            }
            
            $field = empty(input('field'))?'goods_code':trim(input('field'));
            $order = empty(input('sort'))?'DESC':trim(input('sort'));

            $list = [];
            $lists = Db::name('kuerp_goods_sales_summary')->alias('i')
                ->join('kuerp_goods g','g.goods_code=i.goods_code')
                ->join('kuerp_goods_type gt','gt.id=g.goods_type_id')
                // ->join('kuerp_stores s','s.id=i.stores_id')
                // ->join('kuerp_unit u','u.id=i.goods_unit')
                ->where($where)
                ->order(['i.'.$field=>$order])
                ->group('i.goods_code,i.stores_id')
                ->field('sum(i.tax_money) as tax_money,sum(i.money_tax_price) as money_tax_price,sum(i.basic_nmber) as basic_nmber,sum(i.number) as i_number,sum(i.all_money) as all_money,
                ANY_VALUE(i.default_unit) as default_unit,ANY_VALUE(i.goods_unit) as goods_unit,ANY_VALUE(i.purchase_money) as purchase_money,
                ANY_VALUE(i.stores_id) as stores_id,ANY_VALUE(i.unit_price) as unit_price,
                ANY_VALUE(g.goods_code) as goods_code,ANY_VALUE(g.goods_name) as goods_name,ANY_VALUE(g.goods_specification_id) as goods_specification_id,
                ANY_VALUE(g.goods_barcode) as goods_barcode,ANY_VALUE(g.unit_id) as unit_id,ANY_VALUE(g.more_unit) as more_unit,
                ANY_VALUE(gt.supplier_type_name) as supplier_type_name')
                ->select();
                // echo Db::name('kuerp_goods_sales_summary')->getLastSql();
            if(!empty($lists)){
                $unitids = array_column($lists,'unit_id');
                $unitlist = Db::name('kuerp_unit')->where(['id'=> ['in', $unitids]])->column('name', 'id');
                $storeids = array_unique(array_column($lists,'stores_id'));
                $storelist = Db::name('kuerp_stores')->where(['id'=> ['in', $storeids]])->column('name', 'id');
                // $sale_userids = array_unique(array_column($lists,'sale_user'));
                // $sale_userlist = (new StaffMember())->where(['id' => ['in', $sale_userids]])->column('name', 'id');
                foreach ($lists as $lists_k=>$lists_v){
                    $lists_v['i_number'] = $lists_v['basic_nmber'];
                    $unit_conversion = '';
                    $unit = $unitlist[$lists_v['unit_id']] ?? '';
                    if($unit){
                        $lists_v['u_name'] = $unit;
                        $unit_name = $unit;
                        $rowadvance_rate = '1';
                        if($lists_v['more_unit']==1){
                            $more_unit = Db::name('kuerp_unit_conversion')->alias('uc')
                                ->join('kuerp_unit u','u.id=uc.vice_unit')
                                ->where(['uc.main_unit'=>$lists_v['unit_id']])
                                ->order('uc.advance_rate','ASC')
                                ->field('uc.vice_unit,u.name,u.id,uc.advance_rate')
                                ->select();
                            foreach ($more_unit as $unit_k=>$unit_v){
                                $unit_name .= ','.$unit_v['name'];
                                $rowadvance_rate .= ':'.$unit_v['advance_rate'];
                                if($unit_v['id']==$lists_v['goods_unit']){
                                    $lists_v['advance_rate'] = $unit_v['advance_rate'];
                                    $lists_v['more_unit_name'] = $unit_v['name'];
                                }
                            }
                            $unit_conversion = $unit_name.'('.$rowadvance_rate.')';
                        }
                    }
                    $lists_v['unit_conversion'] = $unit_conversion;
                    $lists_v['s_name'] = $storelist[$lists_v['stores_id']] ?? '';
                    // $lists_v['staff_name'] = $sale_userlist[$lists_v['sale_user']] ?? '';

                    if(!empty($list)){
                        foreach ($list as $list_k=>$list_v){
                            if($list_v['goods_code']==$lists_v['goods_code'] && $list_v['stores_id']==$lists_v['stores_id']){
                                $list[$list_k]['i_number'] += $lists_v['i_number'];
                                $list[$list_k]['all_money'] += $lists_v['all_money'];
                                $list[$list_k]['purchase_money'] += $lists_v['purchase_money'];
                                $status = true;
                            }
                        }
                    }
                    if(!isset($status)){
                        $list[] = $lists_v;
                    }
                }
            }

            //获取设置
            $set= \think\Db::name('kuerp_sets')->field('type,name,value,is_checked,set')->select();
            $setList = [];
            foreach ($set as $set_k=>$set_v){
                if(!empty($set_v['set'])){
                    $set_v['set'] = unserialize($set_v['set']);
                }
                $setList[$set_v['name']] = $set_v;
            }
            $count = [
                'i_number'=>0,
                'unit_price'=>0,
                'all_money'=>0,
                'tax_included'=>0,
                'tax_money'=>0,
                'money_tax_price'=>0,
            ];
            $counts = $count;
            $counts['s_name'] = '合计';
            //数量价格格式化
            $number_decimal = $setList['number_decimal']['value'];
            $price_decimal  = $setList['price_decimal']['value'];
            foreach ($list as $list_k=>$list_v){
                if(isset($list_v['advance_rate'])){
                    $list[$list_k]['basic_nmber'] = round( ($list_v['i_number']/$list_v['advance_rate']) ,$number_decimal).$list_v['more_unit_name'];
                }
                if($list_v['i_number']!=0){
                    $list[$list_k]['unit_price'] = round( ($list_v['all_money']/$list_v['i_number']) ,$price_decimal);
                    $list[$list_k]['tax_included'] = round( ($list_v['money_tax_price']/$list_v['i_number']) ,$price_decimal);
                }else{
                    $list[$list_k]['unit_price'] = 0;
                    $list[$list_k]['tax_included'] = 0;
                }
                $list_v['unit_price'] = $list[$list_k]['unit_price'];
                $list_v['tax_included'] = $list[$list_k]['tax_included'];
                
                $list[$list_k]['i_number'] = round( ($list_v['i_number']) ,$number_decimal);
                $list[$list_k]['all_money'] = round( ($list_v['all_money']) ,$price_decimal);
                $list[$list_k]['tax_money'] = round( ($list_v['tax_money']) ,$price_decimal);
                $list[$list_k]['money_tax_price'] = round( ($list_v['money_tax_price']) ,$price_decimal);
                foreach ($count as $count_k=>$count_v){
                    $count[$count_k] += $list_v[$count_k];
                    $counts[$count_k] += $list_v[$count_k];
                }
            }
            $counts['unit_price'] = $counts['i_number']>0?(round($counts['all_money']/$counts['i_number'],$price_decimal)):0;
            $counts['tax_included'] = round($counts['tax_included'],$price_decimal);
            $counts['all_money'] = round($counts['all_money'],$price_decimal);
            $counts['tax_money'] = round($counts['tax_money'],$price_decimal);
            $counts['money_tax_price'] = round($counts['money_tax_price'],$price_decimal);
            $list[] = $counts;
            $result = array("total" => sizeof($list), "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

}
